var e = require;
var t = module;
var n = exports;
var i =
        (this && this.__awaiter) ||
        function (e, a, s, c) {
            return new (s = s || Promise)(function (n, t) {
                function o(e) {
                    try {
                        r(c.next(e));
                    } catch (e) {
                        t(e);
                    }
                }
                function i(e) {
                    try {
                        r(c.throw(e));
                    } catch (e) {
                        t(e);
                    }
                }
                function r(e) {
                    var t;
                    e.done
                        ? n(e.value)
                        : ((t = e.value) instanceof s
                              ? t
                              : new s(function (e) {
                                    e(t);
                                })
                          ).then(o, i);
                }
                r((c = c.apply(e, a || [])).next());
            });
        },
    r =
        (this && this.__generator) ||
        function (n, o) {
            var i,
                r,
                a,
                s = {
                    label: 0,
                    sent: function () {
                        if (1 & a[0]) throw a[1];
                        return a[1];
                    },
                    trys: [],
                    ops: []
                },
                e = {next: t(0), throw: t(1), return: t(2)};
            return (
                "function" == typeof Symbol &&
                    (e[Symbol.iterator] = function () {
                        return this;
                    }),
                e
            );
            function t(t) {
                return function (e) {
                    return (function (t) {
                        if (i) throw new TypeError("Generator is already executing.");
                        for (; s; )
                            try {
                                if (
                                    (1,
                                    (i = 1),
                                    r &&
                                        (a =
                                            2 & t[0]
                                                ? r.return
                                                : t[0]
                                                ? r.throw || ((a = r.return) && a.call(r), 0)
                                                : r.next) &&
                                        !(a = a.call(r, t[1])).done)
                                )
                                    return a;
                                switch (((r = 0), (t = a ? [2 & t[0], a.value] : t)[0])) {
                                    case 0:
                                    case 1:
                                        a = t;
                                        break;
                                    case 4:
                                        return s.label++, {value: t[1], done: !1};
                                    case 5:
                                        s.label++, (r = t[1]), (t = [0]);
                                        continue;
                                    case 7:
                                        (t = s.ops.pop()), s.trys.pop();
                                        continue;
                                    default:
                                        if (
                                            !(a = 0 < (a = s.trys).length && a[a.length - 1]) &&
                                            (6 === t[0] || 2 === t[0])
                                        ) {
                                            s = 0;
                                            continue;
                                        }
                                        if (3 === t[0] && (!a || (t[1] > a[0] && t[1] < a[3]))) {
                                            s.label = t[1];
                                            break;
                                        }
                                        if (6 === t[0] && s.label < a[1]) {
                                            (s.label = a[1]), (a = t);
                                            break;
                                        }
                                        if (a && s.label < a[2]) {
                                            (s.label = a[2]), s.ops.push(t);
                                            break;
                                        }
                                        a[2] && s.ops.pop(), s.trys.pop();
                                        continue;
                                }
                                t = o.call(n, s);
                            } catch (e) {
                                (t = [6, e]), (r = 0);
                            } finally {
                                i = a = 0;
                            }
                        if (5 & t[0]) throw t[1];
                        return {value: t[0] ? t[1] : void 0, done: !0};
                    })([t, e]);
                };
            }
        };
Object.defineProperty(n, "__esModule", {value: !0}), (n.sound = n.Sound = void 0);
var o,
    a = e("const"),
    s = e("events"),
    c = e("user-data");
((e = o = n.Sound || (n.Sound = {})).CLICK = "sound/click"),
    (e.BGM = "sound/bgm"),
    (e.CHOOSECOIN = "sound/chooseCoin"),
    (e.GETAREA = "sound/getArea"),
    (e.RESET = "sound/reset"),
    (e.WARN = "sound/warn"),
    (e.GETNEWCOIN = "sound/getNewCoin"),
    (e.UNLOCKAREA = "sound/unlockArea"),
    (e.MERGE10 = "sound/merge10"),
    (e.RENEWAREA = "sound/renewArea"),
    (e.WIN = "sound/win"),
    (e.TZ_GIVECOIN = "sound/tz_giveCoin"),
    (e.TZ_FULL = "sound/tz_full"),
    (e.MERGE = "sound/merge"),
    (e.PLACECOIN = "sound/placeCoin"),
    (e.GIVECOIN = "sound/giveCoin"),
    (e.RESETSTART = "sound/resetStart"),
    (e.MJ_MERGE = "sound/mj_merge"),
    (e.MJ_PLACECOIN = "sound/mj_placeCoin"),
    (e.MJ_GIVECOIN = "sound/mj_giveCoin"),
    (e.MJ_RESETSTART = "sound/mj_resetStart");
(l.prototype.onSoundChange = function () {
    var e = this;
    clearTimeout(this.delayKey),
        (this.delayKey = setTimeout(function () {
            clearTimeout(e.delayKey), (e.delayKey = 0), e.isSharing || e.isHiding ? e.stopAll() : e.resumeAll();
        }, 100));
}),
    (l.prototype.stopAllEffects = function () {
        cc.audioEngine.stopAllEffects();
    }),
    (l.prototype.resumeAll = function () {
        cc.audioEngine.resumeMusic(), cc.audioEngine.resumeAllEffects(), this.onBGMSwitchChange();
    }),
    (l.prototype.stopAll = function () {
        cc.audioEngine.stopMusic(), cc.audioEngine.stopAllEffects();
    }),
    (l.prototype.play = function (o, n) {
        return (
            void 0 === n && (n = 1),
            i(this, void 0, void 0, function () {
                var t;
                return r(this, function (e) {
                    switch (e.label) {
                        case 0:
                            return !1 === c.userData.getSetting().effectSound || null == o
                                ? [2]
                                : ((n = Math.max(1, Math.min(0, n))),
                                  o instanceof cc.AudioClip ? ((t = o), [3, 4]) : [3, 1]);
                        case 1:
                            return null == this.clips[o] ? [3, 2] : ((t = this.clips[o]), [3, 4]);
                        case 2:
                            return [
                                4,
                                new Promise(function (n) {
                                    cc.resources.load(o, cc.AudioClip, function (e, t) {
                                        n(e ? null : t);
                                    });
                                })
                            ];
                        case 3:
                            (t = e.sent()), (this.clips[o] = t), (e.label = 4);
                        case 4:
                            return t
                                ? (cc.audioEngine.playEffect(t, !1),
                                  cc.audioEngine.setEffectsVolume(n),
                                  [2, t.duration])
                                : [2];
                    }
                });
            })
        );
    }),
    (l.prototype.playBGM = function () {
        return i(this, void 0, void 0, function () {
            return r(this, function (e) {
                switch (e.label) {
                    case 0:
                        return !1 === c.userData.getSetting().bgmSound
                            ? [2]
                            : ((this.bgmID = 0),
                              [
                                  4,
                                  new Promise(function (n) {
                                      cc.resources.load(o.BGM, cc.AudioClip, function (e, t) {
                                          n(e ? null : t);
                                      });
                                  })
                              ]);
                    case 1:
                        return e.sent(), cc.audioEngine.setMusicVolume(1), [2];
                }
            });
        });
    }),
    (l.prototype.stopBGM = function () {
        return i(this, void 0, void 0, function () {
            return r(this, function () {
                return (
                    !1 === c.userData.getSetting().bgmSound ||
                        (0 !== this.bgmID && (cc.audioEngine.stopMusic(), (this.bgmID = 0)),
                        (this.m_specificBgmClip = null)),
                    [2]
                );
            });
        });
    }),
    (l.prototype.playSpecificBGM = function (e) {
        return i(this, void 0, void 0, function () {
            return r(this, function () {
                return (
                    !1 === c.userData.getSetting().bgmSound ||
                        ((this.bgmID = cc.audioEngine.playMusic(e, !0)),
                        cc.audioEngine.setMusicVolume(1),
                        (this.m_specificBgmClip = e)),
                    [2]
                );
            });
        });
    }),
    (l.prototype.onBGMSwitchChange = function () {
        var e = cc.audioEngine.getState(this.bgmID) != cc.audioEngine.AudioState.ERROR;
        c.userData.getSetting().bgmSound
            ? e
                ? cc.audioEngine.resumeMusic()
                : null != this.m_specificBgmClip
                ? this.playSpecificBGM(this.m_specificBgmClip)
                : this.playBGM()
            : e && cc.audioEngine.stopMusic();
    }),
    (e = l);
function l() {
    var t = this;
    (this.isSharing = !1),
        (this.isHiding = !1),
        (this.m_specificBgmClip = null),
        (this.delayKey = 0),
        s.userEvent.on(function (e) {
            null != e && "setting" === e.kind && t.onBGMSwitchChange();
        }),
        s.platformEvent.on(function (e) {
            e.onShow ? ((t.isHiding = !1), t.onSoundChange()) : e.onHide && ((t.isHiding = !0), t.onSoundChange());
        }),
        s.adEvent.on(function (e) {
            (t.isSharing = e == a.AdEvent.SHARE || e == a.AdEvent.VIDEO), t.onSoundChange();
        }),
        (this.clips = {});
}
n.sound = new e();
